D:\a\tools.proto\tools.proto\runtime\src\message\util\common.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::message::{Error, FromBytes, FromBytesWithOffsets, Message, WriteTo}; |
30 | | use bytesutil::ReadBytes; |
31 | | use std::io::Write; |
32 | | use std::marker::PhantomData; |
33 | | |
34 | | pub struct Optional<T>(PhantomData<T>); |
35 | | |
36 | | impl<'a, T: FromBytes<'a>> FromBytes<'a> for Optional<T> { |
37 | | type Output = Option<T::Output>; |
38 | | |
39 | 7 | fn from_bytes(slice: &'a [u8]) -> Result<Message<Option<T::Output>>, Error> { |
40 | 7 | if slice.is_empty() { Branch (40:12): [Folded - Ignored]
Branch (40:12): [True: 0, False: 2]
Branch (40:12): [True: 0, False: 2]
Branch (40:12): [True: 0, False: 2]
Branch (40:12): [True: 0, False: 1]
Branch (40:12): [Folded - Ignored]
|
41 | 0 | Err(Error::Truncated) |
42 | | } else { |
43 | 7 | let b = slice[0] > 0; |
44 | 7 | if b { Branch (44:16): [Folded - Ignored]
Branch (44:16): [True: 1, False: 1]
Branch (44:16): [True: 2, False: 0]
Branch (44:16): [True: 1, False: 1]
Branch (44:16): [True: 1, False: 0]
Branch (44:16): [Folded - Ignored]
|
45 | 5 | let msg = T::from_bytes(&slice[1..])?0 ; |
46 | 5 | Ok(Message::new(msg.size() + 1, Some(msg.into_inner()))) |
47 | | } else { |
48 | 2 | Ok(Message::new(1, None)) |
49 | | } |
50 | | } |
51 | 7 | } |
52 | | } |
53 | | |
54 | | impl<'a, T: FromBytes<'a> + FromBytesWithOffsets<'a>> FromBytesWithOffsets<'a> for Optional<T> { |
55 | | type Offsets = Option<T::Offsets>; |
56 | | |
57 | 1 | fn from_bytes_with_offsets(slice: &'a [u8]) -> crate::message::Result<Message<(Self::Output, Self::Offsets)>> { |
58 | 1 | if slice.is_empty() { Branch (58:12): [Folded - Ignored]
Branch (58:12): [True: 0, False: 1]
Branch (58:12): [Folded - Ignored]
|
59 | 0 | Err(Error::Truncated) |
60 | | } else { |
61 | 1 | let b = slice[0] > 0; |
62 | 1 | if b { Branch (62:16): [Folded - Ignored]
Branch (62:16): [True: 1, False: 0]
Branch (62:16): [Folded - Ignored]
|
63 | 1 | let msg = T::from_bytes_with_offsets(&slice[1..])?0 ; |
64 | 1 | let size = msg.size(); |
65 | 1 | let (msg, offsets) = msg.into_inner(); |
66 | 1 | Ok(Message::new(size + 1, (Some(msg), Some(offsets)))) |
67 | | } else { |
68 | 0 | Ok(Message::new(1, (None, None))) |
69 | | } |
70 | | } |
71 | 1 | } |
72 | | } |
73 | | |
74 | | impl<T: WriteTo> WriteTo for Optional<T> { |
75 | | type Input<'a> = Option<T::Input<'a>>; |
76 | | |
77 | 8 | fn write_to<W: Write>(input: &Self::Input<'_>, mut out: W) -> Result<(), Error> { |
78 | 8 | match input { |
79 | 2 | None => out.write_all(&[0x0])?0 , |
80 | 6 | Some(v) => { |
81 | 6 | out.write_all(&[0x1])?0 ; |
82 | 6 | T::write_to(v, out)?0 ; |
83 | | } |
84 | | } |
85 | 8 | Ok(()) |
86 | 8 | } |
87 | | } |
88 | | |
89 | | #[cfg(feature = "tokio")] |
90 | | impl<T: crate::message::WriteToAsync> crate::message::WriteToAsync for Optional<T> { |
91 | 0 | async fn write_to_async<W: tokio::io::AsyncWriteExt + Unpin>( |
92 | 0 | input: &Self::Input<'_>, |
93 | 0 | mut out: W, |
94 | 0 | ) -> crate::message::Result<()> { |
95 | 0 | match input { |
96 | 0 | None => out.write_all(&[0x0]).await?, |
97 | 0 | Some(v) => { |
98 | 0 | out.write_all(&[0x1]).await?; |
99 | 0 | T::write_to_async(v, out).await?; |
100 | | } |
101 | | } |
102 | 0 | Ok(()) |
103 | 0 | } |
104 | | } |
105 | | |
106 | | #[derive(Debug, Copy, Clone)] |
107 | | pub struct ValueLE<T>(PhantomData<T>); |
108 | | #[derive(Debug, Copy, Clone)] |
109 | | pub struct ValueBE<T>(PhantomData<T>); |
110 | | |
111 | | impl<'a, T: ReadBytes> FromBytes<'a> for ValueLE<T> { |
112 | | type Output = T; |
113 | | |
114 | 25 | fn from_bytes(slice: &'a [u8]) -> Result<Message<Self::Output>, Error> { |
115 | 25 | let size = size_of::<T>(); |
116 | 25 | if slice.len() < size { Branch (116:12): [Folded - Ignored]
Branch (116:12): [True: 0, False: 17]
Branch (116:12): [True: 0, False: 4]
Branch (116:12): [True: 0, False: 4]
Branch (116:12): [Folded - Ignored]
|
117 | 0 | Err(Error::Truncated) |
118 | | } else { |
119 | 25 | let value = T::read_bytes_le(slice); |
120 | 25 | Ok(Message::new(size, value)) |
121 | | } |
122 | 25 | } |
123 | | } |
124 | | |
125 | | impl<T: bytesutil::WriteTo> WriteTo for ValueLE<T> { |
126 | | type Input<'a> = T; |
127 | | |
128 | 25 | fn write_to<W: Write>(input: &Self::Input<'_>, out: W) -> Result<(), Error> { |
129 | 25 | input.write_to_le(out)?0 ; |
130 | 25 | Ok(()) |
131 | 25 | } |
132 | | } |
133 | | |
134 | | #[cfg(feature = "tokio")] |
135 | | impl<T: bytesutil::WriteTo + bytesutil::WriteBytes> crate::message::WriteToAsync for ValueLE<T> { |
136 | 0 | async fn write_to_async<W: tokio::io::AsyncWriteExt + Unpin>( |
137 | 0 | input: &Self::Input<'_>, |
138 | 0 | mut out: W, |
139 | 0 | ) -> crate::message::Result<()> { |
140 | 0 | let mut buffer = [0; 8]; |
141 | 0 | T::write_bytes_le(input, &mut buffer); |
142 | 0 | out.write_all(&buffer[..size_of::<T>()]).await?; |
143 | 0 | Ok(()) |
144 | 0 | } |
145 | | } |
146 | | |
147 | | impl<'a, T: ReadBytes> FromBytes<'a> for ValueBE<T> { |
148 | | type Output = T; |
149 | | |
150 | 0 | fn from_bytes(slice: &'a [u8]) -> Result<Message<Self::Output>, Error> { |
151 | 0 | let size = size_of::<T>(); |
152 | 0 | if slice.len() < size { Branch (152:12): [Folded - Ignored]
Branch (152:12): [Folded - Ignored]
|
153 | 0 | Err(Error::Truncated) |
154 | | } else { |
155 | 0 | let value = T::read_bytes_be(slice); |
156 | 0 | Ok(Message::new(size, value)) |
157 | | } |
158 | 0 | } |
159 | | } |
160 | | |
161 | | impl<T: bytesutil::WriteTo> WriteTo for ValueBE<T> { |
162 | | type Input<'a> = T; |
163 | | |
164 | 0 | fn write_to<W: Write>(input: &Self::Input<'_>, out: W) -> Result<(), Error> { |
165 | 0 | input.write_to_be(out)?; |
166 | 0 | Ok(()) |
167 | 0 | } |
168 | | } |
169 | | |
170 | | #[cfg(feature = "tokio")] |
171 | | impl<T: bytesutil::WriteTo + bytesutil::WriteBytes> crate::message::WriteToAsync for ValueBE<T> { |
172 | 0 | async fn write_to_async<W: tokio::io::AsyncWriteExt + Unpin>( |
173 | 0 | input: &Self::Input<'_>, |
174 | 0 | mut out: W, |
175 | 0 | ) -> crate::message::Result<()> { |
176 | 0 | let mut buffer = [0; 8]; |
177 | 0 | T::write_bytes_be(input, &mut buffer); |
178 | 0 | out.write_all(&buffer[..size_of::<T>()]).await?; |
179 | 0 | Ok(()) |
180 | 0 | } |
181 | | } |